<?php
namespace app\wechat\controller;
use think\Db;
use Wechat\Wechat;

class Accept
{
    public function index()
    {
        file_put_contents('wechat.txt', date('Y-m-d H:i:s') . "\n ");
        $weixinconfig = config('wechat');
        $weixin = new Wechat($weixinconfig);
        $weixin->valid();
        $type = $weixin->getRev()->getRevType();  // 消息类型
        $wxid = $weixin->getRev()->getRevFrom();  // 用户openid
        file_put_contents('wechat.txt', $wxid . "\n ", FILE_APPEND);
        $reMsg = "";
        $event = ['event' => ''];
        file_put_contents('wechat.txt', $type . "\n ", FILE_APPEND);
        switch($type) {
            case 'text':
                $content = $weixin->getRev()->getRevContent();
                file_put_contents('wechat.txt', $content . "\n ", FILE_APPEND);
                break;
            case 'event':
                $event = $weixin->getRev()->getRevEvent();
                $content =  json_encode($event);
                break;
            case 'image':
                $content = json_encode($weixin->getRev()->getRevPic());
                $reMsg = "图片很美！";
                break;
            case 'location':
                $content = json_encode($weixin->getRev()->getRevGeo());
                // $reMsg = "您所在的位置很安全！";
                break;
            default:
                $content = '';
                $reMsg = '未知消息类型';
        }
        file_put_contents('wechat.txt', $reMsg . "--reMsg \n ", FILE_APPEND);
        if($reMsg){
            file_put_contents('wechat.txt',"reMsg \n ", FILE_APPEND);
            echo $weixin->text($reMsg)->reply();exit;
        }

        if ($event['event'] == "subscribe") { //用户关注
            $data = db('scan_log')->where('openid', $wxid)->order('id desc')->find();
            if ($data) {
                if ($data['action'] == 'distribution_') {
                    $newsData = array(
                        [
                            'Title'=>'注册会员',
                            'Description'=>'有推荐人推荐您注册摩邦会员！！',
                            'PicUrl'=>'http://' . $_SERVER['HTTP_HOST'] . '/static/wechat/images/deng1_03.jpg',
                            'Url'=>'http://' . $_SERVER['HTTP_HOST'] . url('w_distribution/register', ['phone' => $data['p_phone'], 'id' => $data['p_id']])
                        ]
                    );
                    echo $weixin->news($newsData)->reply(); exit;
                }
                if ($data['action'] == 'gathering_') {
                    $newsData = array(
                        [
                            'Title'=>'扫码支付',
                            'Description'=>'您有一笔待支付款项！！',
                            'PicUrl'=>'http://' . $_SERVER['HTTP_HOST'] . '/static/wechat/images/deng1_03.jpg',
                            'Url'=>'http://' . $_SERVER['HTTP_HOST'] . url('r_wallet/repair_gathering', ['repair_id' => $data['repair_id']])
                        ]
                    );
                    echo $weixin->news($newsData)->reply(); exit;
                }
            }
            echo $weixin->text('欢迎来到摩邦车队，老司机带你飞1！！！')->reply(); exit;
        }
        file_put_contents('wechat.txt', "--unsubscribe \n ", FILE_APPEND);
        if ($event['event'] == "unsubscribe"){ // 取消关注
            file_put_contents('wechat.txt', "--unsubscribe-- \n ", FILE_APPEND);
            exit;
        }

        file_put_contents('wechat.txt', json_encode($event) . "--event \n ", FILE_APPEND);
        //判断用户是否点击的菜单
        if ($event['event'] == "CLICK"){
            file_put_contents('wechat.txt', 'aaa' . "\n ", FILE_APPEND);
            $content = $event['key'];
            switch($content){
                case "best":
                case "new":
                case "hot":
                    $newsData = array();
                    echo $weixin->news($newsData)->reply();exit;
                    break;
                case "ddcx":
                    $text = '';
                    echo $weixin->text($text)->reply();exit;
                    break;
                case "jcbd":

                    break;
                case "info":
                    echo $weixin->text('')->reply();exit;
                    break;
                case "qd":
                    break;
                case 'qdcx':
                    break;
                default:
                    echo $weixin->text("未定义菜单事件{$content}")->reply();exit;
                    break;
            }
        }
        //$content = $api->getstr($content);
        //处理用户扫一扫
        file_put_contents('wechat.txt', 'SCAN' . "\n ", FILE_APPEND);
        if ($event['event'] == "SCAN"){
            file_put_contents('wechat.txt', 'bbb' . "\n ", FILE_APPEND);
            $content = intval($event['key']);//场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000

            echo $weixin->text('扫码业务处理')->reply();exit;
        }

        file_put_contents('wechat.txt', $content . "\n ", FILE_APPEND);
        file_put_contents('wechat.txt', date('Y-m-d H:i:s') . "\n ", FILE_APPEND);
        //处理用户的输入
        if($content){
            if($content == '1'){
                echo $weixin->text('你说的有道理啊！！！')->reply();exit;
            }
            if($content == '客服'){
                echo $weixin->text('客服mm来了')->reply();exit;
            }
        }
    }
}
